//
//  MeatMarketListVIewController.m
//  PigMarket
//
//  Created by 张敬 on 2019/7/15.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "MeatMainListViewController.h"
#import "MeatBuyViewController.h"
#import "MeatSearchViewController.h"
#import "MeatCityFriendsViewController.h"
#import "MeatMineViewController.h"


#import "MeatMainHeader.h"

#import "MeatCityViewController.h"


@interface MeatMainListViewController ()

@property (nonatomic, strong) NSArray *titles;
@property (nonatomic, strong) NSArray *controls;
@property (nonatomic, strong) MeatMainHeader *header;

@end

@implementation MeatMainListViewController

#pragma mark - view life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
}

- (void)viewWillLayoutSubviews{
    [super viewWillLayoutSubviews];
    self.contentScrollView.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight);
}

#pragma mark - init method

- (void)setUpView {
    [self setupNavBar];
    [self setupChildViewControllers];
    [self setupChildSetting];
}

- (void)setupNavBar{
    [self.view addSubview:self.header];
}

- (void)setupChildViewControllers{

    kWeakSelf(self);
    [self.controls enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

        UIViewController *vc = (UIViewController *)obj;
        vc.title = weakself.titles[idx];
        [weakself addChildViewController:vc];
    }];
}

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    kWeakSelf(self);
    [self setUpContentViewFrame:^(UIView *contentView) {
        CGFloat contentX = 0;
        CGFloat contentH = KScreenHeight - weakself.header.height - TJ_TabBarHeight;
        contentView.frame = CGRectMake(contentX, weakself.header.height, KScreenWidth, contentH);
    }];
}

- (void)setupChildSetting
{
    kWeakSelf(self);
    [self setUpContentViewFrame:^(UIView *contentView) {
        CGFloat contentX = 0;
        CGFloat contentH = KScreenHeight - weakself.header.height - TJ_TabBarHeight ;
        contentView.frame = CGRectMake(contentX, weakself.header.height, KScreenWidth, contentH);
    }];
    
    [self setUpTitleEffect:^(UIColor *__autoreleasing *titleScrollViewColor, UIColor *__autoreleasing *norColor, UIColor *__autoreleasing *selColor, UIFont *__autoreleasing *titleFont, CGFloat *titleHeight,CGFloat *titleWidth) {
        
        // 设置标题字体
        *titleFont = mFont(15);
        *norColor = UIColorFromRGB(0x333333);
        *selColor = mMainColor;
        *titleHeight = 42;
        *titleScrollViewColor = KWhiteColor;
    }];
    
    // 推荐方式（设置下标）
    [self setUpUnderLineEffect:^(BOOL *isUnderLineDelayScroll, CGFloat *underLineH, UIColor *__autoreleasing *underLineColor,BOOL *isUnderLineEqualTitleWidth) {
        // 标题填充模式
        *underLineColor = mMainColor;
        *isUnderLineEqualTitleWidth = YES;
        *underLineH = 3;
    }];
    
}

#pragma mark - setter and getter
- (NSArray *)titles{
    if (!_titles) {
        _titles = @[@"卖肉",@"同城掌柜",@"我的肉铺"];
    }
    return _titles;
}

- (NSArray *)controls
{
    if (!_controls) {
        
        MeatBuyViewController *oneVC = [[MeatBuyViewController alloc] init];
        MeatCityViewController *twoVC = [[MeatCityViewController alloc]init];
        MeatMineViewController *threeVC = [[MeatMineViewController alloc]init];
        _controls = @[oneVC,twoVC,threeVC];
    }
    return _controls;
}


- (MeatMainHeader *)header{
    if (!_header) {
        _header = [[MeatMainHeader alloc] init];
        kWeakSelf(self);
        _header.dealSearchBlock = ^{
            
            //点击搜索按钮
            MeatSearchViewController * vc = [[MeatSearchViewController alloc]init];
//            vc.hotWords = _model.data.hotWords;
//            kWeakSelf(self)
//            vc.choosedCellBlock = ^(XXHomeStoresModel * _Nonnull model) {
//                XXHomeShopDetailViewController * vc = [[XXHomeShopDetailViewController alloc] init];
//                vc.sid = model.storeId;
//                [weakself.navigationController pushViewController:vc animated:YES];
//            };
            [weakself presentViewController:vc animated:YES completion:nil];

        };
    }
    return _header;
}

@end
